<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function findBrandByCategory($id){
    return db('pro_category')->where('id',$id)->value('brand_id');
}
function findBrandBySeries($id){
    return db('pro_category t')
        ->join('pro_series t2','t2.category_id=t.id')
        ->where('t2.id',$id)->value('t.brand_id');
}
function findBrandBySku($id){
    return db('pro_category t')
        ->join('pro_series t2','t2.category_id=t.id')
        ->join('pro_sku t3','t3.series_id=t2.id')
        ->where('t3.id',$id)->value('t.brand_id');
}
function findBrandByProduct($id){
    return db('pro_category t')
        ->join('pro_series t2','t2.category_id=t.id')
        ->join('pro_sku t3','t3.series_id=t2.id')
        ->join('pro_products t4','t4.sku_id=t3.id')
        ->where('t4.id',$id)->value('t.brand_id');
}
//对象转成数组
function object_array($array) {
    if(is_object($array)) {
        $array = (array)$array;
    } if(is_array($array)) {
        foreach($array as $key=>$value) {
            $array[$key] = object_array($value);
        }
    }
    return $array;
}

if (!function_exists('download')) {
    function download($file_url, $new_name = '')
    {
//        if (!isset($file_url) || trim($file_url) == '') {
//            echo '500';
//        }
//        if (!file_exists($file_url)) { //检查文件是否存在
//            echo '404';
//        }

        ob_clean();

        $file_name = basename($file_url);
        $file_secs = explode('.', $file_url);
        $file_type = end($file_secs);
        $file_name = trim($new_name) == '' ? $file_name : urlencode($new_name) . '.' . $file_type;
//        $file_name = iconv("utf-8","gb2312", $file_name);
        ob_end_clean();
        //输入文件标签
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: " . filesize($file_url));
        header("Content-Disposition: attachment; filename=" . $file_name);

        $file = fopen($file_url, 'rb'); //打开文件
        //输出文件内容
        while (!feof($file)) {
            echo fread($file, 8192);
        }
//        echo fread($file, filesize($file_url));
        fclose($file);
        die();
    }
}

function delToken($data){
    if(isset($data['token'])){
        unset($data['token']);
    }
    return $data;
}